package com.wztk.shydemo.proxy;

import com.fi.common.http.FiHttpRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created by win7 on 2016/7/7.
 */
public class ProxyManager {
    private final String DOMAIN = "http://127.0.0.1:8080/sys/getProxyInfo";
    private final String FIJSON = "fiJson";
    private static ProxyRequestArgs proxyRequestArgs = new ProxyRequestArgs();
    private ProxyManagerArgs proxyManagerAags;

    //执行线程类
    private static final class ScriptRunner extends Thread {
        public ScriptRunner(String command, boolean isRoot){
            String[] commandsArray =  command.split("\n");
            ShellUtils.CommandResult result = ShellUtils.execCommand(commandsArray, isRoot);
        }
        @Override
        public void run() {

        }
    }

    //网络请求类
    protected class HttpRequestThread implements Callable<String> {
        private String url;
        ProxyRequestArgs proxyRequestArgs;
        private final String FIJSON = "fiJson";
        public HttpRequestThread(String url, ProxyRequestArgs proxyRequestArgs){
            this.url = url;
            this.proxyRequestArgs = proxyRequestArgs;
        }
        private String getProxyInfo() throws UnsupportedEncodingException {
            Gson gs = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
            HashMap<String, String> hsMap = new HashMap<String, String>();
            hsMap.put(FIJSON, gs.toJson(proxyRequestArgs));
            String ret = FiHttpRequest.sendGet(this.url, hsMap);
            return ret;
        }

        @Override
        public String call() throws Exception {
            return getProxyInfo();
        }
    }

    /**
     * @title: ProxyManagerAars类
     * @description: 此类用来做一些必要配置参数的传递，并无其他作用
     */
    public static class ProxyManagerArgs{
        private String serverUrl;       //代理请求URL
        private String domain;          //代理提供商域名
        private String tid;             //tid订单编号
        private String area;            //请求IP地区
        private String num;             //请求IP数
        private String workdir;         //redsock工作目录
        private String uid;             //程序UID
        private String port1;           //redsocks本地重定向端口1
        private String port2;           //redsocks本地重定向端口2

        public void setServerUrl(String serverUrl) {
            this.serverUrl = serverUrl;
        }

        public void setDomain(String domain) {
            this.domain = domain;
        }

        public void setTid(String tid) {
            this.tid = tid;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public void setNum(String num) {
            this.num = num;
        }

        public void setWorkdir(String workdir) {
            this.workdir = workdir;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public void setPort1(String port1) {
            this.port1 = port1;
        }

        public void setPort2(String port2) {
            this.port2 = port2;
        }

        public String getServerUrl() {
            return serverUrl;
        }

        public String getDomain() {
            return domain;
        }

        public String getTid() {
            return tid;
        }

        public String getArea() {
            return area;
        }

        public String getNum() {
            return num;
        }

        public String getWorkdir() {
            return workdir;
        }

        public String getUid() {
            return uid;
        }

        public String getPort1() {
            return port1;
        }

        public String getPort2() {
            return port2;
        }
    }

    public class Daili66Ret{
        private String host;
        private String port;
        private String area;
        private String operator;

        public void setHost(String host) {
            this.host = host;
        }

        public void setPort(String port) {
            this.port = port;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public void setOperator(String operator) {
            this.operator = operator;
        }

        public String getHost() {
            return host;
        }

        public String getPort() {
            return port;
        }

        public String getArea() {
            return area;
        }

        public String getOperator() {
            return operator;
        }
    }


    /**
     * @title : ProxyManager
     * @return
     * @description: 构造函数传入必须的参数
     */
    public ProxyManager(ProxyManagerArgs proxyManagerArgs){
        this.proxyManagerAags = proxyManagerArgs;
        proxyRequestArgs.setDomain(proxyManagerArgs.getDomain());
        proxyRequestArgs.setTid(proxyManagerArgs.getTid());
        proxyRequestArgs.setArea(proxyManagerArgs.getArea());
        proxyRequestArgs.setNum(proxyManagerArgs.getNum());
    }

    public boolean RunCommand(String command, boolean isRoot) {
        String[] commandsArray =  command.split("\n");
        ShellUtils.CommandResult result = ShellUtils.execCommand(commandsArray, isRoot);
        if (0 == result.result){
            return true;
        }else{
            return false;
        }
    }

    public static void stopProxy(String workdir){
        RedsocksCmds.stopRedsocks(workdir);
    }
    public  boolean setProxy() throws InterruptedException {
        String ret = "";

        //代理IP的的获取
        HttpRequestThread task = new HttpRequestThread(proxyManagerAags.getServerUrl(), this.proxyRequestArgs);
        FutureTask<String> futureTask = new FutureTask<String>(task);
        Thread thread = new Thread(futureTask);
        thread.start();
        thread.join();
        try {
            ret = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        if ("" == ret) {
            return false;
        } else {
            //命令行的执行
            Gson gson = new Gson();
            Daili66Ret[] dl66 = gson.fromJson(ret, Daili66Ret[].class);
//            RedsocksCmds redsocksCmds = new RedsocksCmds(proxyManagerAags.getWorkdir(), proxyManagerAags.getUid(), dl66[0].getHost(),
//                    dl66[0].getPort(), proxyManagerAags.getPort1(), proxyManagerAags.getPort2());
            RedsocksCmds redsocksCmds = new RedsocksCmds(proxyManagerAags.getWorkdir(), proxyManagerAags.getUid(), "192.168.0.101",
                    "8888", proxyManagerAags.getPort1(), proxyManagerAags.getPort2());
            //redsocksCmds.createRedsocksCfg();
            redsocksCmds.startRedsocks();
            return true;
        }
    }
}
